package com.terrynoya.fight.combat
{
	import com.terrynoya.fight.char.Character;
	import com.terrynoya.fight.util.Misc;
	import com.terrynoya.geom.MVector2D;

	public class CharacterBind
	{
		private var _chara:Character;
		private var _bindCharacter:Character;
		private var _time:int;
		private var _offset:MVector2D;
		private var _facingFlag:int;
		private var _isActive:Boolean;
		private var _isTargetBind:Boolean;
		
		public function CharacterBind(chara:Character)
		{
			this._chara = chara;
			this._offset = new MVector2D();
		}
		
		public function get isTargetBind():Boolean
		{
			return _isTargetBind;
		}

		public function get isActive():Boolean
		{
			return _isActive;
		}

		public function update():void
		{
			if(this._isActive && (this._time == -1 || this._time > 0))
			{
				trace(this._chara.id," bind update:->",this._isActive,this._time);
				this.updateTime();
				this.bind();
			}
			else
			{
				this.reset();
			}
		}
		
		private function reset():void
		{
			this._bindCharacter = null;
			this._time = 0;
			this._offset = new MVector2D();
			this._isActive = false;
			this._isTargetBind = false;
		}
		
		private function updateTime():void
		{
			if(this._time > 0)
			{
				this._time --;
			}
		}
		
		public function get bindTo():Character
		{
			return _bindCharacter;
		}

		public function set(bindChara:Character,offset:MVector2D,time:int,facingFlag:int,targetBind:Boolean):void
		{
			this._bindCharacter = bindChara;
			this._offset = offset;
			this._time = time;
			this._facingFlag = facingFlag;
			this._isTargetBind = targetBind;
			this._isActive = true;
		}
		
		private function bind():void
		{
			this._chara.position = Misc.getOffset(this.bindTo.position,this.bindTo.currentFacing,this._offset);
			this._chara.velocity = this.bindTo.velocity;
			this._chara.acceleration = this.bindTo.acceleration;
			
			if(this._facingFlag > 0)
			{
				this._chara.facing = this.bindTo.currentFacing;
			}
			else if(this._facingFlag < 0)
			{
				this._chara.facing = Misc.flipFacing(this.bindTo.currentFacing);	
			}
		}
	}
}